package controller.UserManagement;

import entity.UserInfForEn;
import entity.UserInfForStu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import entity.User;
import service.EnterpriseManagement.EnterpriseService;
import service.UserManagement.UserService;
import util.ResponseMessage;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/User")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private EnterpriseService enterpriseService;

    @RequestMapping("/")
    public String index() {
        return "account/login";
    }

    @RequestMapping("/Welcome")
    public String welcome() {
        return "account/welcome";
    }

    @CrossOrigin(allowCredentials = "true")
    @ResponseBody
    @PostMapping("/Login")
    public ResponseMessage login(String userId, String password, HttpServletRequest request) {
        HttpSession session = request.getSession();
        User userLogin = new User();
        userLogin.setUserId(userId);
        userLogin.setPassWord(password);
        boolean flag = userService.login(userLogin);
        if (flag) {
            User user = userService.findUser(userId);
            //设置session
            session.setAttribute("user", user);
            session.setAttribute("user_id", user.getUserId());
            LinkedHashMap<String,Object> map = new LinkedHashMap<String,Object>();
            map.put("User",user);
            map.put("JSESSIONID",request.getSession().getId());
            return new ResponseMessage(ResponseMessage.SUCCESS, "登录成功",map);
        } else {
            return new ResponseMessage(ResponseMessage.ERROR, "用户名或密码错误");
        }
    }

    @CrossOrigin(allowCredentials = "true")
    @ResponseBody
    @PostMapping("/LoginOut")
    public ResponseMessage LoginOut(HttpSession session) {
        session.invalidate();
        return new ResponseMessage(ResponseMessage.SUCCESS, "退出成功");
    }

    /**
     * 用户注册（差用户头像）
     *
     * @param userId   用户手机号
     * @param password 密码
     * @param type     用户类型
     */
    @CrossOrigin(allowCredentials = "true")
    @ResponseBody
    @PostMapping("/Register")
    public ResponseMessage register(String userId, String password, String name, int type) {
        User flag = userService.findUser(userId);
        if (flag != null) {
            return new ResponseMessage(ResponseMessage.ERROR, "手机号已被注册，注册失败");
        }
        User user = new User();
        user.setUserId(userId);
        user.setPassWord(password);
        user.setName(name);
        user.setType(type);
        user.setIcon("http://47.96.104.134:8080/oneschool/images/default-avatar.jpg");
//        System.out.println(user);
        boolean add_flag = userService.register(user);
        return new ResponseMessage(ResponseMessage.SUCCESS, "用户注册成功", user);
    }

    @CrossOrigin(allowCredentials = "true")
    @ResponseBody
    @PostMapping("/BindStuInfo")
    public ResponseMessage bindStu(String userinformationId, String name, String sex, String email, String school, String major, String timeforgraduation, String degree, HttpServletRequest request) {
        HttpSession session = request.getSession();
        User Current_logged_in_user = (User) session.getAttribute("user");
        System.out.println("Current_logged_in_user = " + Current_logged_in_user);
        System.out.println((String) session.getAttribute("user_id"));
        if (Current_logged_in_user == null) {
            return new ResponseMessage(ResponseMessage.ERROR, "用户未登录");
        }
        UserInfForStu userInfForStu = new UserInfForStu();
        userInfForStu.setUserInformationId(userinformationId);
        userInfForStu.setName(name);
        userInfForStu.setPhoneNumber(Current_logged_in_user.getUserId());
        userInfForStu.setSex(sex);
        userInfForStu.setEmail(email);
        userInfForStu.setSchool(school);
        userInfForStu.setMajor(major);
        userInfForStu.setTimeForGraduation(timeforgraduation);
        userInfForStu.setDegree(degree);
//        System.out.println(userInfForStu);
//        System.out.println("m=" + userInfForStu.getUserInformationId());
//        System.out.println("u=" + Current_logged_in_user.getUserId());
        boolean flag2 = userService.addUserInformationId(userInfForStu.getUserInformationId(), Current_logged_in_user.getUserId());
        if (flag2) {
            boolean flag = userService.BindUserInfForStu(userInfForStu);
            if (flag) {

                return new ResponseMessage(ResponseMessage.SUCCESS, "学生信息绑定成功", userInfForStu);
            }
            return new ResponseMessage(ResponseMessage.ERROR, "没有此学生信息");
        }
        return new ResponseMessage(ResponseMessage.ERROR, "系统错误", userInfForStu);
    }

    @CrossOrigin(allowCredentials = "true")
    @ResponseBody
    @PostMapping("/BindEnInfo")
    public ResponseMessage bindEn(String userinformationId, String name, String sex, String email, String enterpriseName, String degree, String time, HttpServletRequest request) {
        HttpSession session = request.getSession();
        User Current_logged_in_user = (User) session.getAttribute("user");
        if (Current_logged_in_user == null) {
            return new ResponseMessage(ResponseMessage.ERROR, "用户未登录");
        }
//        String tempuserId = "18995630965";
        UserInfForEn userInfForEn = new UserInfForEn();
        userInfForEn.setUserInformationId(userinformationId);
        userInfForEn.setName(name);
        userInfForEn.setPhoneNumber(Current_logged_in_user.getUserId());
//        userInfForEn.setPhoneNumber(tempuserId);

        if (enterpriseService.findEnterpriseIdByName(enterpriseName) != null) {
            userInfForEn.setEnterpriseId(enterpriseService.findEnterpriseIdByName(enterpriseName).getEnterpriseId());
        } else {
            return new ResponseMessage(ResponseMessage.ERROR, "公司名输入错误");
        }
        userInfForEn.setSex(sex);
        userInfForEn.setEmail(email);
        userInfForEn.setDegree(degree);
        userInfForEn.setTime(time);
        System.out.println(userInfForEn);
        boolean flag2 = userService.addUserInformationId(userInfForEn.getUserInformationId(), Current_logged_in_user.getUserId());
//        boolean flag2 = userService.addUserInformationId(userInfForEn.getUserInformationId(), tempuserId);
//        System.out.println("userInfForEn.getUserInformationId() = "+userInfForEn.getUserInformationId());
//        boolean flag2 = userService.addUserInformationId(userInfForEn.getUserInformationId(),tempuserId);
        if (flag2) {
            boolean flag = userService.BindUserInfForEn(userInfForEn);
            if (flag) {
                return new ResponseMessage(ResponseMessage.SUCCESS, "员工信息绑定成功");
            }
            return new ResponseMessage(ResponseMessage.ERROR, "没有此员工信息");
        }

        return new ResponseMessage(ResponseMessage.ERROR, "系统错误");
    }

    @CrossOrigin(allowCredentials = "true")
    @ResponseBody
    @PostMapping("/showStu")
    public ResponseMessage showStu(HttpServletRequest request) {
        HttpSession session = request.getSession();
        User Current_logged_in_user = (User) session.getAttribute("user");
        if (Current_logged_in_user == null) {
            return new ResponseMessage(ResponseMessage.ERROR, "用户未登录");
        }
        if (Current_logged_in_user.getType() != 1) {
            return new ResponseMessage(ResponseMessage.ERROR, "用户不是学生用户");
        }
        UserInfForStu userInfForStu = userService.showUser(Current_logged_in_user.getUserId());
        return new ResponseMessage(ResponseMessage.SUCCESS, "获取成功",userInfForStu);
    }

    @CrossOrigin(allowCredentials = "true")
    @ResponseBody
    @PostMapping("/showEnr")
    public ResponseMessage showEnr(HttpServletRequest request) {
        HttpSession session = request.getSession();
        User Current_logged_in_user = (User) session.getAttribute("user");
        if (Current_logged_in_user == null) {
            return new ResponseMessage(ResponseMessage.ERROR, "用户未登录");
        }
        if (Current_logged_in_user.getType() != 3) {
            return new ResponseMessage(ResponseMessage.ERROR, "用户不是企业人员");
        }
        LinkedHashMap<String,Object> map = userService.showEnr(Current_logged_in_user.getUserId());
        return new ResponseMessage(ResponseMessage.SUCCESS, "获取成功", map);
    }

} // end class
